package com.example.yam.greenapp;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ClistActivity extends AppCompatActivity {

    //数据
    private final String TAG="ClistActivity";
    private String url = MyApplication.localhost+"Green/index.php/indexg/clist/id/";

    private String cid = null;
    private String cname = null;
    private int page = 0;//页号
    private List<Goods> goodsList = new ArrayList<Goods>();//数据列表

    //适配器
    private  GoodsAdapter goodsAdapter = null;
    //控件
    private ListView listView = null;
    private SwipeRefreshLayout swipeRefreshLayout = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clist);

        //工具栏--返回
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }


        this.listView = (ListView) super.findViewById(R.id.clist_lv);
        this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent it = new Intent(ClistActivity.this,DetailActivity.class);
                it.putExtra("id",goodsList.get(position).getId());
                Log.v(TAG,goodsList.get(position).getId());
                startActivity(it);
            }
        });
        this.swipeRefreshLayout = (SwipeRefreshLayout) super.findViewById(R.id.clist_swip);

        //添加监听事件
        this.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                Log.v(TAG,"刷新");
                ClistActivity.this.page+=5;

                //设置列表数据
                String nurl =  ClistActivity.this.url+ ClistActivity.this.cid+"/stnum/"+ ClistActivity.this.page;
                //设置数据列表
                VolleyGetString(nurl);
                ClistActivity.this.goodsAdapter.notifyDataSetChanged();
                ClistActivity.this.swipeRefreshLayout.setRefreshing(false);
            }
        });

        init();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void init(){
        Intent it = getIntent();
        this.cid = it.getStringExtra("cid");
        this.cname = it.getStringExtra("cname");

        //设置列表数据
        String nurl = this.url+this.cid+"/stnum/"+this.page;
        //设置数据列表
        this.goodsAdapter = new GoodsAdapter(ClistActivity.this,R.layout.goodslist,goodsList);
        VolleyGetString(nurl);
    }

    //使用volley获取数据
    private void VolleyGetString(String url){
        RequestQueue mQueue = MyApplication.getHttpQueue();
        Log.d(TAG, "开始了");
        StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {//正确搜索到数据
                        parseJSONobject(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, error.getMessage(), error);
            }
        });
        mQueue.add(stringRequest);
    }
    //json解析
    private void parseJSONobject(String ss){
        try{
            // Log.d(TAG,"数据是"+ss);
            JSONObject jsb = new JSONObject(ss);
            JSONArray jsonArray=(JSONArray) new JSONArray(jsb.getString("goods"));
            //排行榜数据布置
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jb=jsonArray.getJSONObject(i);
                String pic=MyApplication.localhost+"Green/public/photo/"+jb.getString("pic");
                Log.v(TAG,pic);
                Log.v(TAG,jb.getString("name"));
                Goods g = new Goods(jb.getString("id"),pic, jb.getString("name"), jb.getString("lintro"),
                        jb.getString("danwei"), jb.getString("price"));
                goodsList.add(g);
            }
            this.listView.setAdapter(this.goodsAdapter);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
